﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
   <title>Print dialog</title>
   <script language="javascript" type="text/javascript">

      var count = 0;
      var totalPages = 0;

      function printPages() {

         if (isChrome()) {
            window.resizeTo(800, 600);
         }
         window.print();
      }

      function uploadPages() {
         //get storage key
         var storageKey = getQueryVariable("sk");
         //totalPages = getQueryVariable("tp");
         totalPages = this.PdfDocument.Pages.length;

         count = 0;

         for (var pageIndex = 0; pageIndex < totalPages; pageIndex++) {

            //get the info about the 
            var page = this.PdfDocument.Pages[pageIndex];
            var rotation = page.rotation ? page.rotation : 0;

            getPage(storageKey, pageIndex, rotation);
         }
      }

      function sleep(milliSeconds) {
         var startTime = new Date().getTime(); // get the current time
         while (new Date().getTime() < startTime + milliSeconds) {
         }  // hog cpu
      }

      function getPage(storageKey, pageNumber, rotation) {

         //create an image and sign up to load event
         var page = new Image();
         page.onload = preloadHandler;
         if (pageNumber != totalPages - 1) {
            page.className = "page pageBreak";
         }
         else {
            page.className = "page";
         }

         var hexRotation = convertRotation(rotation);

         //get the value of the 'su' param from the URL
         var serviceName = getQueryVariable("su");
         //get the full path to the service
         var serviceUrl = getFullServiceUrl(serviceName);

         page.src = serviceUrl + '?&it=print&b=' + storageKey + '&a=' + pageNumber + '&r=' + hexRotation;

         var placeholder = document.getElementById("imagesPlaceholder");
         placeholder.appendChild(page);
      }

      function convertRotation(rotation) {

         var bin = '';
         switch (rotation) {
            case 90:
               bin += '01';
               break;
            case 180:
               bin += '10';
               break;
            case 270:
               bin += '11';
               break;
            default:
               bin += '00';
               break;
         }
         //convert the binary data to hex
         var dec = parseInt(bin, 2);
         var hex = dec.toString(16);

         return hex;
      }

      function preloadHandler() {
         count++;

         //calculate a size for the pages
         //since the DPI was increased 3 times.
         var w = (this.width / (72 * 3)).toFixed(2);

         if (w > 7.5) {
            w = (w * 0.85).toFixed(2);
         }

         this.style.width = w + 'in';

         var curPage = document.getElementById("curPageNum");
         curPage.innerHTML = count + '';

         var totPage = document.getElementById("totalPageNum");
         totPage.innerHTML = totalPages + '';

         if (count == totalPages) {
            var message = document.getElementById("msg");
            message.innerHTML = totalPages + ' pages are ready';

            var printBtn = document.getElementById("printButton");
            printBtn.disabled = false;
         }
      }

      function getFullServiceUrl(serviceName) {
         //now we need to get the 'http://localhost:49374/TestSite' part of
         //current location 'http://localhost:49374/TestSite/WebResource.axd?su=pageasimage.ashx'
         //for that purpose lets get the 'TestSite/WebResource.axd?su=pageasimage.ashx' part
         var path = window.location.pathname + window.location.search;
         //now remove 'TestSite' part
         path = path.replace(/^\/[^\/]+/, '');

         //and finally get the 'http://localhost:49374/TestSite' by replacing the rest with empty string
         var url = window.location.href;
         url = url.replace(path, '');
         url = url.replace("#", '');

         return url + '/' + serviceName;
      }

      function getQueryVariable(variable) {
         var query = window.location.search.substring(1);
         var vars = query.split("&");
         for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
               return pair[1];
            }
         }
         alert('Query Variable ' + variable + ' not found');
      }

      function isIE() {
         var dataString = navigator.userAgent;
         this.versionSearchString = "MSIE" || "Explorer";
         if (dataString) {
            if (dataString.indexOf("MSIE") != -1)
               return true;
         }
         return false;
      }
      
      function isChrome() {
         var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
         return is_chrome;
      }


   </script>
   <style>
      @media all
      {
         .page
         {
            display: none;
         }
      }
      @media print
      {
         body, #content, #container
         {
            height: 99%;
            margin: 0;
            padding: 0;
            float: none;
            background: #fff url(none);
         }
         
         .page
         {
            display: block;
            /*height: 100%;*/
            /*width: 7.5in;*/
            margin: 0;
            padding: 0;
            float: none;
            background: #fff url(none);
         }
      
         .pageBreak
         {
            page-break-after: always;
         }
      
         .screenOnly
         {
            display: none;
            visibility: collapse;
         }
      }
   </style>
</head>
<body>
   <div class="screenOnly" style="text-align: center">
      <div>
         <span id="msg">Preparing page <span id="curPageNum"></span>/<span id="totalPageNum"></span></span>
      </div>
      <button id="printButton" disabled onclick="printPages(); return false;">
         Print</button>
   </div>
   <div id="imagesPlaceholder">
   </div>
   <script language="javascript" type="text/javascript">
      if (!isIE()) {
         uploadPages();
      }
   </script>
</body>
</html>
